﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static AccountOperations.AccountOperations;
using static LoanOperations.LoanOperations;

namespace Bank_database_system.业务部.账户操作
{

    public partial class 信息查询c : UserControl
    {
        public event EventHandler ButtonBack;
        public 信息查询c()
        {
            InitializeComponent();
        }

        public void CardID_Set(string cardID)
        {
            textBox5.Text = cardID;
            textBox1.Text = cardID;
            DataTable dataTable = BankCardInquiry(cardID);
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                DataRow row = dataTable.Rows[0];
                string date = row["OPEN_DATE"].ToString();
                string state = row["STATE"].ToString();
                string branch = row["BRANCH_ID"].ToString();
                string ID = row["IDENTIFICATION_NUMBER"].ToString();
                string balance = row["BALANCE"].ToString();
                string name = CustomerNameInquiryByID(ID);

                textBox2.Text = date;
                textBox3.Text = state;
                textBox4.Text = branch;
                textBox6.Text = name;
                textBox8.Text = balance;
                if (state == "冻结")
                {
                    textBox7.Text = "0";
                }
                else
                {
                    textBox7.Text = balance;
                }
            }
            else
            {
                MessageBox.Show("查询失败，请稍后再试！");
                button4_Click(this, EventArgs.Empty);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            ButtonBack?.Invoke(this, EventArgs.Empty);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "正常")
            {
                if (BankCardOperate(textBox1.Text, OperateNumber.freeze))
                {
                    MessageBox.Show("卡号" + textBox5.Text + "冻结成功！");
                    textBox7.Text = "0";
                    textBox3.Text = "冻结";
                }
                else
                {
                    MessageBox.Show("操作失败，请稍后重试！");
                }
            }
            else
            {
                MessageBox.Show("卡号" + textBox5.Text + "已经冻结，无须操作！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox3.Text == "冻结")
            {
                if (BankCardOperate(textBox1.Text, OperateNumber.unfreeze))
                {
                    //后端数据同步
                    MessageBox.Show("卡号" + textBox5.Text + "解冻成功！");
                    textBox7.Text = textBox8.Text;
                    textBox3.Text = "正常";
                }
                else
                {
                    MessageBox.Show("密码错误，请重新输入！");
                }
            }
            else
            {
                MessageBox.Show("卡号" + textBox5.Text + "未冻结，无须操作！");
            }
        }

        private bool CheckDepositState(DataTable dataTable)
        {
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow row = dataTable.Rows[i];
                string state = row["STATE"].ToString();
                if (state == "定期中")
                {
                    return false;
                }
            }
            return true;
        }

        private bool CheckDeposit()
        {
            DataTable dataTable = GetDepositByCardID(textBox1.Text);
            if (dataTable == null || dataTable.Rows.Count == 0)
            {
                return true;
            }
            else if (!CheckDepositState(dataTable))
            {
                MessageBox.Show("卡号" + textBox5.Text + "有未结束的存款，不能销户！");
                return false;
            }
            return true;
        }

        private static bool CheckLoanState(DataTable dataTable)
        {
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow row = dataTable.Rows[i];
                string state = row["STATE"].ToString();
                if (state == "还款中")
                {
                    return false;
                }
            }
            return true;
        }

        private bool CheckLoan()
        {
            DataTable dataTable = GetLoansByCardID(textBox1.Text);
            if (dataTable == null || dataTable.Rows.Count == 0)
            {
                return true;
            }
            else if (!CheckLoanState(dataTable))
            {
                MessageBox.Show("卡号" + textBox5.Text + "有未结束的贷款，不能销户！");
                return false;
            }
            else
            {
                return true;
            }
        }

        private bool CheckBalance()
        {
            string balance = BankCardBalanceInquiry(textBox1.Text);
            if (balance == null || balance == "")
            {
                return false;
            }
            else
            {
                decimal balanceNum = decimal.Parse(balance);
                if (balanceNum > 0)
                {
                    MessageBox.Show("卡号" + textBox5.Text + "可用余额，不能销户！");
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        private bool CheckTransferState(DataTable dataTable)
        {
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow row = dataTable.Rows[i];
                string state = row["STATE"].ToString();
                if (state == "转账中")
                {
                    return false;
                }
            }
            return true;
        }

        private bool CheckTransfer()
        {
            DataTable dataTable = GetTransferByCardID(textBox1.Text);
            if (dataTable == null || dataTable.Rows.Count == 0)
            {
                return true;
            }
            else if (!CheckTransferState(dataTable))
            {
                MessageBox.Show("卡号" + textBox5.Text + "有未完成的转账，不能销户！");
                return false;
            }
            else
            {
                return true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (CheckLoan() && CheckDeposit() && CheckBalance() && CheckTransfer())
            {
                if (BankCardOperate(textBox1.Text, OperateNumber.accountClosure))
                {
                    //后端数据同步
                    MessageBox.Show("卡号" + textBox5.Text + "已销户！");
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                    textBox5.Text = "";
                    textBox6.Text = "";
                    textBox7.Text = "";
                    textBox8.Text = "";
                    ButtonBack?.Invoke(this, EventArgs.Empty);
                }
            }
        }
    }
}
